package com.heima.behavior.controller;

import com.heima.behavior.dto.BehaviorDto;
import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.util.UserThreadLocalUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/v1/behavior")
@Api(tags = "APP行为实体表,一个行为实体可能是用户或者设备，或者其它接口")
public class BehaviorController {


    @Autowired
    private StringRedisTemplate redisTemplate;

    /*请求 URL: http://192.168.58.1:8083/app/behavior/api/v1/behavior/load_article_behavior/
    请求方法: POST
    {"equipmentId":"88888888","articleId":"1543199991928025089","authorId":4}
    {
    "code": 0,
    "errorMessage": null,
    "data": {
        "islike": false,
        "isunlike": false,
        "iscollection": false,
        "isfollow": false
            }
    }*/
    @PostMapping("/load_article_behavior")
    @ApiOperation(value = "用户行为回显", notes = "author:liJie")  // value指名称  notes 备注
    @ApiImplicitParam(name = "dto", value = "接收参数", required = true, dataType = "BehaviorDto")
    public ResponseResult loadArticleBehavior(@RequestBody BehaviorDto dto) {
        User user = UserThreadLocalUtil.get();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        dto.setUserId(user.getUserId());
        // 定义返回的结果
        Map<String, Boolean> map = new HashMap<>();
        log.info("[用户行为微服务======>测试，接收参数：{}]",redisTemplate.hasKey("saveFollow"+"_"+dto.getUserId()+"_"+dto.getEquipmentId()+"_"+dto.getArticleId()));
        map.put("isfollow", redisTemplate.hasKey("saveFollow"+"_"+dto.getUserId()+"_"+dto.getEquipmentId()+"_"+dto.getArticleId()));
        map.put("islike", redisTemplate.hasKey("saveLikes"+"_"+dto.getUserId()+"_"+dto.getEquipmentId()+"_"+dto.getArticleId()));
        map.put("isunlike", redisTemplate.hasKey("saveUnlikes"+"_"+dto.getUserId()+"_"+dto.getEquipmentId()+"_"+dto.getArticleId()));
        map.put("iscollection", redisTemplate.hasKey("saveCollection"+"_"+dto.getUserId()+"_"+dto.getEquipmentId()+"_"+dto.getArticleId()));
        return ResponseResult.okResult(map);
    }
}
